package com.dgl.sdk.view.advert;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class MyViewPager extends ViewPager {

	private float mLastMotionX;     
    private float mLastMotionY;
    private boolean isScrolling = false;
    private boolean left = false;  
    private boolean right = false;  
    private int lastValue = -1;
    
	public MyViewPager(Context context) {
		super(context);
		//setOnPageChangeListener(listener);
	}
	
	public MyViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
		//setOnPageChangeListener(listener);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		
		final int action = e.getAction();     
        final float x = e.getX();     
        final float y = e.getY();  
        switch (action) {     
        case MotionEvent.ACTION_DOWN:
        	if((getCurrentItem() == 0 && right) || (getCurrentItem() == getAdapter().getCount()-1 && left)){
        		left = right = false;
        		getParent().requestDisallowInterceptTouchEvent(false);
        	} else {
        		getParent().requestDisallowInterceptTouchEvent(true);
        	}
            mLastMotionX = x;     
            mLastMotionY = y;
            break;               
        case MotionEvent.ACTION_MOVE:
            final float dx = x - mLastMotionX;     
            final float xDiff = Math.abs(dx);
            final float yDiff = Math.abs(y - mLastMotionY);
            if (xDiff > 10 && xDiff > yDiff) {
            	if((getCurrentItem() == 0 && dx > 0) || (getCurrentItem() == getAdapter().getCount()-1 && dx < 0)){
            		getParent().requestDisallowInterceptTouchEvent(false);	
            	}
                mLastMotionX = x;     
            } else if(yDiff > 10 && yDiff > xDiff) {
            	getParent().requestDisallowInterceptTouchEvent(false);
            }         
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
        	getParent().requestDisallowInterceptTouchEvent(false);
            break;        	
        }
		return super.onInterceptTouchEvent(e);
	}
	
	public  OnPageChangeListener listener = new OnPageChangeListener(){

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			if (arg0 == 1) {
				isScrolling = true;
			} else {
				isScrolling = false;
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			if (isScrolling) {  
                if (lastValue > arg2) {  
                    right = true;  
                    left = false;  
                } else if (lastValue < arg2) {  
                    right = false;  
                    left = true;  
                } else if (lastValue == arg2) {  
                    right = left = false;  
                }  
            }   
            lastValue = arg2; 
		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
		}		
	};
}
